package com.ouzexi.finalexp

import android.content.Intent
import android.os.Bundle
import android.os.Looper
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.Gson
import com.ouzexi.finalexp.vo.registerReturn
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.activity_register.*
import kotlinx.android.synthetic.main.activity_register.password
import okhttp3.*
import okhttp3.FormBody
import java.io.IOException


class register : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)
    }

    fun register(view: View) {

        val that = this
        // 获取输入信息
        val email = email.text
        val password = password.text
        val confirmPwd = confirmPwd.text
        val code = verifyCode.text

        // 判断
        if(email.toString().isEmpty()||password.toString().isEmpty()||confirmPwd.toString().isEmpty()||code.toString().isEmpty()){
            Toast.makeText(this,"请输入必填项",Toast.LENGTH_SHORT).show()
        } else {
            if(password.toString() != confirmPwd.toString()) {
                Toast.makeText(this,"两次密码输入不一致",Toast.LENGTH_SHORT).show()
            } else {
                val urlAPI = "http://10.0.2.2:8080/school/student/register"

                val requestBody: RequestBody = FormBody.Builder()
                    .add("username", email.toString())
                    .add("code", code.toString())
                    .add("password", password.toString())
                    .build()

                val request: Request = Request.Builder()
                    .url(urlAPI)
                    .post(requestBody)
                    .build()

                val client = OkHttpClient()
                client.newCall(request).enqueue(object : Callback {

                    override fun onResponse(call: Call, response: Response) {
                        val result = response.body?.string()
                        println("result:$result")
                        val dataObj: registerReturn = Gson().fromJson(result, registerReturn::class.java)
                        if(dataObj.code == -1) {
                            Log.i("code",dataObj.code.toString())
                            Log.i("test","注册失败")
                            Looper.prepare()
                            Toast.makeText(that,dataObj.msg,Toast.LENGTH_SHORT).show()
                            Looper.loop()
                        }
                        else {
                            if (dataObj.msg == "success") {
                                Log.i("test","注册成功")
                                // 与数据库匹配
                                Looper.prepare()
                                Toast.makeText(that,"注册成功",Toast.LENGTH_SHORT).show()
                                val intent = Intent(that, login::class.java)
                                startActivity(intent)
                                Looper.loop()
                            } else {
                                Log.i("test","注册失败")
                                // 与数据库匹配
                                Looper.prepare()
                                Toast.makeText(that,"系统错误，请稍后重试",Toast.LENGTH_SHORT).show()
                                Looper.loop()
                            }
                        }
                    }

                    override fun onFailure(call: Call, e: IOException) {
                        println("Failed request api :( " + e.message)
                    }
                })
            }
        }
    }

    fun sendCode(view: View) {
        val email = email.text
        val urlAPI = "http://10.0.2.2:8080/school/student/send"

        val requestBody: RequestBody = FormBody.Builder()
            .add("to", email.toString())
            .build()

        val request: Request = Request.Builder()
            .url(urlAPI)
            .post(requestBody)
            .build()

        val client = OkHttpClient()
        client.newCall(request).enqueue(object : Callback {

            override fun onResponse(call: Call, response: Response) {
                val result = response.body?.string()
                println("result:$result")
                Looper.prepare()
                Toast.makeText(this@register,"发送成功",Toast.LENGTH_SHORT).show()
                Looper.loop()
            }

            override fun onFailure(call: Call, e: IOException) {
                println("Failed request api :( " + e.message)
            }
        })
    }
}